package com.mas.framework.common.pojo;

import com.mas.framework.common.exception.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * @author godyao
 * @description 通用返回体
 * @date 2023年01月19日17:39
 */
@Data
public class CommonResult<T> implements Serializable {
    /**
     * 错误码
     */
    private Integer code;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 错误提示，用户可阅读
     */
    private String msg;

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.code = 200;
        result.data = data;
        result.msg = "";
        return result;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }

    public static <T> CommonResult<T> error(Integer code, String message) {
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.msg = message;
        return result;
    }
}
